import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/homeView.vue'
import Welcome from '../components/Welcome'
import GearIndex from "@/views/ordering/gearIndex.vue";
import ScoreList from "@/views/ordering/scoreList.vue";
import OrderingIndex from '@/views/ordering/index.vue'
import OrderPost from "@/views/ordering/orderPost.vue";
import UserIndex from '@/views/users/index.vue'
import OrderList from "@/views/ordering/orderList.vue";
import Login from "@/views/login/index.vue";
import {getStore} from "js-utils";
import UserPanel from "@/views/users/userPanel.vue";
import MealManager from "@/views/ordering/mealManager.vue";
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    // redirect: '/welcome',
    redirect: '/orderingIndex',
    component: {render: c => c('router-view')},//https://blog.csdn.net/Marin6/article/details/122185133
    children: [//因为要在首页的页面嵌套显示welcome页面，所以需要以子路由的形式
      {path: '/welcome', component: Welcome,meta:{auth:true}},
      {path: '/gearsManage', component: GearIndex,meta:{auth:true}},
      {path: '/orderingIndex', component: OrderingIndex,meta:{auth:true}},
      {path: '/orderPost', component: OrderPost,meta:{auth:true}},
      {path: '/orderList', component: OrderList,meta:{auth:true}},
      {path: '/userIndex', component: UserIndex,meta:{auth:true}},
      {path: '/scoreList', component: ScoreList,meta:{auth:true}},
      {path: '/userPanel', component: UserPanel,meta:{auth:true}},
      {path: '/mealManager', component: MealManager,meta:{auth:true}},
    ],
    meta:{auth:true}
  },
  {
    path:'/login',
    name: 'login',
    component: Login
  }
]

const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach((to,from,next)=>{
  let token=getStore('token',true)
  // 判断该页面是否需要登录
  if(to.meta.auth){
    if(token){
      next()
    }else{
      next({
        path:'/login',
        // 跳转时传递参数到登录页面，以便登录后可以跳转到对应页面
        query:{
          redirect:to.fullPath
        }
      })
    }
  }else{
    next()
  }
})
export default router
